package cn.yunhe.insurance.controller;


import cn.yunhe.insurance.entity.Branch;
import cn.yunhe.insurance.entity.Emp;
import cn.yunhe.insurance.service.IbranchService;
import cn.yunhe.insurance.util.DateUtil;
import cn.yunhe.insurance.util.Result;
import cn.yunhe.insurance.util.UUIDGenerator;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 姜威
 * @date 2018/9/14 16:46
 */

@RestController
@RequestMapping("/branch")
public class BranchController {


    @Resource
    private IbranchService branchService;

    @RequestMapping("tobranchcount")
    public ModelAndView toBranchCount(@RequestParam String buuid){
        Map<String,Object> map=new HashMap<String, Object>();
        Branch branch=branchService.findBranchByBuuid(buuid);
        map.put("branch",branch);
        return new ModelAndView("branch_count",map);
    }
    @RequestMapping("tomap")
    public ModelAndView toMap(){
        return new ModelAndView("map");
    }
    @RequestMapping("/tobranchlist")
    public ModelAndView toBranchList(){
        return new ModelAndView("branch_list");
    }
    @RequestMapping("/toadd")
    public ModelAndView toAdd(){
        return new ModelAndView("branch_add");
    }
    @RequestMapping("/getBranchList")
    public Map findLikeBranchs(int pageNo, int pageSize, Branch branch,String  keyword){
        Map<String,Object> map=new HashMap<String, Object>();

        map.put("pageNo",pageNo);
        map.put("pageSize",pageSize);
        map.put("branch",branch);
        map.put("keyword",keyword);
        Page<Branch> page=branchService.findLikeBranchs(map);
        map.put("page",page);
        map.put("totalPage",page.getPages());
        return map;
    }
    @RequestMapping("/add")
    public Result addBranch(Branch branch){
        String buuid=UUIDGenerator.getUUID();
        String bstartdate=DateUtil.date2StringDateTime(new Date());
        branch.setBstartdate(bstartdate);
        branch.setBuuid(buuid);
        branchService.addBranch(branch);
        return new Result();
    }
    @RequestMapping("/getBranch")
    public ModelAndView getBranchByBuuid(Branch branch){
        Branch branch1=branchService.findBranchByBuuid(branch.getBuuid());
        Map map=new HashMap();

        map.put("branch",branch1);
        return  new ModelAndView("branch_edit",map);
    }
    @RequestMapping("/update")
    public Result updateBranch(Branch branch){
        branchService.updateBranch(branch);
        return new Result();
    }
    @RequestMapping("/updateTodel")
    public Result delBranch(Branch branch){
        branch.setBflag(1);
        branchService.delBranch(branch);
        return new Result();
    }
    @RequestMapping("/tobranchmap")
    public ModelAndView toBranchMap(){
        ModelAndView mv=new ModelAndView();
        List<Branch> list=branchService.findAllBranch();
        mv.addObject("list",list);
        mv.setViewName("branch_map");
        return mv;
      /* return new ModelAndView("branch_map");增加*/
    }
  /*  @RequestMapping("/getbranchmap")
    public Map getBranchMap(){
        Map<String,Object> map=new HashMap<String, Object>();
        List<Branch> list=branchService.findAllBranch();
        map.put("list",list);
        return map;
    }*/
    @RequestMapping("/findEmpByUname")
    public List findEmpByUname(@RequestParam String uname){
       List<Emp> list=branchService.findUserByUname(uname);
       return  list;
    }

    public IbranchService getBranchService() {
        return branchService;
    }

    public void setBranchService(IbranchService branchService) {
        this.branchService = branchService;
    }
}
